<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增汽车')" />
    <th:block th:include="include :: select2-css" />
</head>
<style>
    .divImg {
        float: left;
        margin: 2px;
        padding: 2px;
        height: 108px;
        width: 100px;
        border: 1px solid #dcdcdc;
        cursor: pointer;
    }
</style>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-car-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">名称：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">品牌选择：</label>
                <div class="col-sm-8">
                    <select id="brandId" name="brandId" class="form-control m-b">
                        <option th:each="dict : ${brand}" th:text="${dict.name}" th:value="${dict.id}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">车型选择：</label>
                <div class="col-sm-8">
                    <select id="carType" name="carType" class="form-control m-b">
                        <option>请先选择品牌</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">价格/万：</label>
                <div class="col-sm-8">
                    <input name="price" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">主图(578*277)：</label>
                <div class="col-sm-8">
                    <button type="button" class="layui-btn" id="upload_main">上传图片</button>
                    <div class="layui-upload-list">
                        <input type="hidden" id="mainUrl" name="mainUrl">
                        <p id="show_main"></p>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label"><span style="color: red">*</span>轮播图(750*422)</label>
                <div class="col-sm-8">
                    <div style="text-align: left;">
                        <button type="button" class="layui-btn" id="upload_car_img0" >上传图片</button>
                    </div>
                    <div class="layui-upload-list">
                        <div id="img-url0" style="padding: 5px;"></div>
                        <!--<input type="hidden" id="logo" name="logo">-->
                        <!--<p id="show_img"></p>-->
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label"><span style="color: red">*</span>外观图(690*600)</label>
                <div class="col-sm-8">
                    <div style="text-align: left;">
                        <button type="button" class="layui-btn" id="upload_car_img1" >上传图片</button>
                    </div>
                    <div class="layui-upload-list">
                        <div id="img-url1" style="padding: 5px;"></div>
                        <!--<input type="hidden" id="logo" name="logo">-->
                        <!--<p id="show_img"></p>-->
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label"><span style="color: red">*</span>内饰图(690*600)</label>
                <div class="col-sm-8">
                    <div style="text-align: left;">
                        <button type="button" class="layui-btn" id="upload_car_img2" >上传图片</button>
                    </div>
                    <div class="layui-upload-list">
                        <div id="img-url2" style="padding: 5px;"></div>
                        <!--<input type="hidden" id="logo" name="logo">-->
                        <!--<p id="show_img"></p>-->
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label"><span style="color: red">*</span>科技图(690*600)</label>
                <div class="col-sm-8">
                    <div style="text-align: left;">
                        <button type="button" class="layui-btn" id="upload_car_img3" >上传图片</button>
                    </div>
                    <div class="layui-upload-list">
                        <div id="img-url3" style="padding: 5px;"></div>
                        <!--<input type="hidden" id="logo" name="logo">-->
                        <!--<p id="show_img"></p>-->
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label"><span style="color: red">*</span>配置图(690*600)</label>
                <div class="col-sm-8">
                    <div style="text-align: left;">
                        <button type="button" class="layui-btn" id="upload_car_img4" >上传图片</button>
                    </div>
                    <div class="layui-upload-list">
                        <div id="img-url4" style="padding: 5px;"></div>
                        <!--<input type="hidden" id="logo" name="logo">-->
                        <!--<p id="show_img"></p>-->
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label"><span style="color: red">*</span>权益选择图(490*270)</label>
                <div class="col-sm-8">
                    <div style="text-align: left;">
                        <button type="button" class="layui-btn" id="upload_car_img5" >上传图片</button>
                    </div>
                    <div class="layui-upload-list">
                        <div id="img-url5" style="padding: 5px;"></div>
                        <!--<input type="hidden" id="logo" name="logo">-->
                        <!--<p id="show_img"></p>-->
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">活动选择：</label>
                <div class="col-sm-8">
                    <select name="activityId" class="form-control m-b"  >
                        <option value="">无</option>
                        <option th:each="dict : ${activity}" th:text="${dict.name}" th:value="${dict.id}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group" id="showpriceN">
                <label class="col-sm-3 control-label is-required">排序：</label>
                <div class="col-sm-8">
                    <input name="sort" class="form-control" type="text">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('car_status')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status"
                               th:value="${dict.dictValue}" th:checked="${dict.default}">
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: sortable-js"/>
    <script type="text/javascript">

        $(document).ready(function () {
            updateCarType()
        })

        $("#brandId").on("change",function(){
            updateCarType();
        });

        function updateCarType() {
            var brandId = $("#brandId").val();
            //执行 Ajax
            $.ajax({
                url : prefix+"/type/"+brandId+"/all",
                type : "get",
                success : function(result) {
                    if(result.code==0){
                        var ss = "";
                        $.each(result.data,function (index,value) {
                            ss+="<option value='"+value.id+"'>"+value.name+"</option>"
                        })
                        $("#carType").html(ss);
                    }
                },
                error:function () {
                    layer.msg('失败',{'icon':2});
                }
            });
        }

        var prefix = ctx + "system/car"
        $("#form-car-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-car-add').serialize());
            }
        }

        layui.use('upload', function () {
            var $ = layui.jquery
                , upload = layui.upload;

            //普通图片上传
            upload.render({
                elem: '#upload_main'
                , url: ctx+'/common/upload' //改成您自己的上传接口
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#show_main").html("<img src='" + res.url + "' style='width:80px;height:80px;'>");
                    $("#mainUrl").val(res.url);
                }
            });

            var imgId0 = 0;
            var container0 = document.getElementById("img-url0");
            var sort0 = Sortable.create(container0);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_car_img0'
                , url: ctx+'/common/upload' //改成您自己的上传接口
                , multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name= file.name.split(".")[0];
                        console.log(name)
                        $("#img-url0").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgId0++;
                    $("#div"+res.name).replaceWith("<div id='imgId0" + imgId0 + "' class='divImg'>" +
                        "<img  src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='carImg0' value='" + res.url + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId0" + imgId0 + "')\"></i></div>");

                    sort0.destroy();
                    sort0 = Sortable.create(container0);


                }
            });


            var imgId1 = 0;
            var container1 = document.getElementById("img-url1");
            var sort1 = Sortable.create(container1);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_car_img1'
                , url: ctx+'/common/upload' //改成您自己的上传接口
                , multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name= file.name.split(".")[0];
                        console.log(name)
                        $("#img-url1").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgId1++;
                    $("#div"+res.name).replaceWith("<div id='imgId1" + imgId1 + "' class='divImg'>" +
                        "<img src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='carImg1' value='" + res.url + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId1" + imgId1 + "')\"></i></div>");

                    sort1.destroy();
                    sort1 = Sortable.create(container1);

                }
            });

            var imgId2 = 0;
            var container2 = document.getElementById("img-url2");
            var sort2 = Sortable.create(container2);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_car_img2'
                , url: ctx+'/common/upload' //改成您自己的上传接口
                , multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name= file.name.split(".")[0];
                        console.log(name)
                        $("#img-url2").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgId2++;
                    $("#div"+res.name).replaceWith("<div id='imgId2" + imgId2 + "' class='divImg'>" +
                        "<img src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='carImg2' value='" + res.url + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId2" + imgId2 + "')\"></i></div>");

                    sort2.destroy();
                    sort2 = Sortable.create(container2);

                }
            });

            var imgId3 = 0;
            var container3 = document.getElementById("img-url3");
            var sort3 = Sortable.create(container3);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_car_img3'
                , url: ctx+'/common/upload' //改成您自己的上传接口
                , multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name= file.name.split(".")[0];
                        console.log(name)
                        $("#img-url3").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgId3++;
                    $("#div"+res.name).replaceWith("<div id='imgId3" + imgId3 + "' class='divImg'>" +
                        "<img src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='carImg3' value='" + res.url + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId3" + imgId3 + "')\"></i></div>");

                    sort3.destroy();
                    sort3 = Sortable.create(container3);

                }
            });


            var imgId4 = 0;
            var container4 = document.getElementById("img-url4");
            var sort4 = Sortable.create(container4);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_car_img4'
                , url: ctx+'/common/upload' //改成您自己的上传接口
                , multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name= file.name.split(".")[0];
                        console.log(name)
                        $("#img-url4").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgId4++;
                    $("#div"+res.name).replaceWith("<div id='imgId4" + imgId4 + "' class='divImg'>" +
                        "<img src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='carImg4' value='" + res.url + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId4" + imgId4 + "')\"></i></div>");

                    sort4.destroy();
                    sort4 = Sortable.create(container4);

                }
            });



            var imgId5 = 0;
            var container5 = document.getElementById("img-url5");
            var sort5 = Sortable.create(container5);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_car_img5'
                , url: ctx+'/common/upload' //改成您自己的上传接口
                , multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name= file.name.split(".")[0];
                        console.log(name)
                        $("#img-url5").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgId5++;
                    $("#div"+res.name).replaceWith("<div id='imgId5" + imgId5 + "' class='divImg'>" +
                        "<img src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='carImg5' value='" + res.url + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId5" + imgId5 + "')\"></i></div>");

                    sort5.destroy();
                    sort5 = Sortable.create(container5);

                }
            });




        });

        function delImg(id) {
            $("#" + id).remove();
        }
    </script>
</body>
</html>